<template>
  <!-- action="https://jsonplaceholder.typicode.com/posts/" -->

  <el-upload
    class="avatar-uploader"
    action="/dev-api/web/v1/files/img/upload"
    :headers="headers"
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img
      v-if="imageUrl"
      :src="getImgUrl(imageUrl)"
      class="avatar"
    >
    <i
      v-else
      class="el-icon-plus avatar-uploader-icon"
    />
  </el-upload>
</template>

<script>
import { getToken } from "@/utils/auth";


export default {
  name: "AggImgall",
  props: ['imageUrl'],
  data() {
    return {
      // imageUrl: "",
      headers: {
        accessToken: getToken(),
        token: getToken()
        // token为系统访问需要的,放在header中
      }
    };
  },
  created() {
    // const id = this.$route.params.src;
    // this.imageUrl = src;
    // console.log(imageUrl);
  },
  methods: {
    getImgUrl(imgUrl) {
      console.log("...................." + imgUrl);
      // return "http://km-onlie-images.oss-cn-beijing.aliyuncs.com/" + imgUrl;
      return "http://kamoimgs.oss-cn-beijing.aliyuncs.com/" + imgUrl;

      // https://kamoimgs.oss-cn-beijing.aliyuncs.com/13636851672-driverpaper-1555579735792.png
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
       console.log(this);
      console.log(res);
       console.log(file);
      // this.imageUrl = res.data.imgUrl;
      this.$emit("imgUrl", res.data.filename,this._vnode.elm.id);
    },
    resetImg() {
      console.log("reset");
      this.imageUrl = "";
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      // if (!isJPG) {
      //   this.$message.error("上传头像图片只能是 JPG 格式!");
      // }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      // return isJPG && isLt2M;
      return isLt2M;
    }
  }
};
</script>
<style>
.avatar-uploader {
  width: 450px;
  height: 260px;
  margin: 20px auto;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 40px;
  color: #8c939d;
  width: 260px;
  height: 260px;
  line-height: 260px;
  text-align: center;
}
.avatar {
  width: 260px;
  height: 260px;
  display: block;
}
</style>
